import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {GoodsSummary, QueryGoodsRequest, QueryGoodsResponse} from "../apis/GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {ItemOf} from "../types/EnumArray";
import {OffsetDateTime} from "@js-joda/core";

export function createDirectSaleOrder(clientId: number) {
    return fetcher.post<number>(`directSaleClients/${clientId}/orders`)
}

export function getDirectSaleOrderDetail(id: number) {
    return fetcher.get<DirectSaleOrderDetail>(`directSaleClients/orders/${id}`, {
        includeItems: true
    })
}

export function getDirectSaleOrderDetailWithoutItems(id: number) {
    return fetcher.get<DirectSaleOrderDetail>(`directSaleClients/orders/${id}`)
}

export interface DirectSaleOrderDetail {
    id: number
    createdDateTime: OffsetDateTime
    createdBy: NumberIdAndName
    client: NumberIdAndName
    items: Array<DirectSaleOrderItem>
    submitDateTime?: OffsetDateTime
    paidTimes: number
    paidPrice: number
    shouldPaidPrice: number
}

export interface DirectSaleOrderItem extends QueryGoodsResponse {
    amountInOrder: number
}

export interface AddDirectSaleOrderItemRequest {
    amount: number
    goodsId: string
    price: number
}

export function addDirectSaleOrderItem(id: number, addDirectSaleOrderItemRequest: AddDirectSaleOrderItemRequest) {
    return fetcher.post(`directSaleClients/orders/${id}/items`, {}, addDirectSaleOrderItemRequest)
}

export function deleteDirectSaleOrderItem(orderId: number, goodsId: number) {
    return fetcher.delete(`directSaleClients/orders/${orderId}/items/${goodsId}`)
}

export interface UpdateDirectSaleOrderItemAmountRequest {
    amount: number
}

export function updateDirectSaleOrderItemAmount(orderId: number, goodsId: number, updateDirectSaleOrderItemAmountRequest: UpdateDirectSaleOrderItemAmountRequest) {
    return fetcher.patch(`directSaleClients/orders/${orderId}/items/${goodsId}/amount`, {}, updateDirectSaleOrderItemAmountRequest)
}

export interface QueryDirectSaleOrderRequest {
    clientIds: number[]
    startDate?: string
    endDate?: string
    employeeIds: number[]
}

export function queryDirectSaleOrders(queryDirectSaleOrderRequest: QueryDirectSaleOrderRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryDirectSaleOrderResponse>>("directSaleClients/orders", {...queryDirectSaleOrderRequest, ...pageable})
}

export interface QueryDirectSaleOrderResponse {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    goodsKindAmount: number
    client: NumberIdAndName
    paidPrice: number
    shouldPaidPrice: number
    submittedDateTime: OffsetDateTime
}


export function getAllDirectSaleOrderCreatedEmployees() {
    return fetcher.get<Array<NumberIdAndName>>("directSaleClients/orders/employees", {projection: "summary"})
}

export function submitDirectSaleOrder(id: number) {
    return fetcher.patch<number>(`directSaleClients/orders/${id}/submit`)
}


export function findAllGoodsWithHistoryOrder(id: number) {
    return fetcher.get<GoodsSummaryWithPrice[]>(`directSaleClients/${id}/goods`, {
        projection: "withHistoryOrder",
    })
}

export interface GoodsSummaryWithPrice extends GoodsSummary {
    price: number
    rememberedPrice?: number
}

interface PayDirectSaleOrderRequest {
    price: number
}

export function payDirectSaleOrder(id: number, request: PayDirectSaleOrderRequest) {
    return fetcher.post(`directSaleClients/orders/${id}/pay`,{},request)
}


export function getPayRecordsById(orderId:number){
    return fetcher.get<Array<PaidRecordResponse>>(`directSaleClients/orders/${orderId}/paidRecords`)
}

export interface PaidRecordResponse{
    id:number
    fee:number
    createdDateTime: OffsetDateTime
}